package semSimilarity;

import java.util.ArrayList;
import java.util.List;


public class SimilarityMatrix<L> {
	
	public SimilarityGenerator<L> generator;
	public List<? extends L> list1;
	public List<? extends L> list2;
	double[][] matrix ;
	
	public SimilarityMatrix(List<? extends L> l1, List<? extends L> l2,
			SimilarityGenerator<L> generator) {
		super();
		this.list1 = l1;
		this.list2 = l2;
		this.generator = generator;
		this.matrix = new double[l1.size()][l2.size()];
		for (int i=0; i<this.matrix.length;i++) 
			for (int j=0; j<this.matrix[0].length; j++) {
				matrix[i][j] = this.generator.similarity(l1.get(i), l2.get(j));
				
					
			}
	}

	public SimilarityGenerator<L> getGenerator() {
		return generator;
	}

	public List<? extends L> getList1() {
		return list1;
	}

	public List<? extends L> getList2() {
		return list2;
	}


	public void setGenerator(SimilarityGenerator<L> generator) {
		this.generator = generator;
	}

	public void setList1(List<L> list1) {
		this.list1 = list1;
	}
	
	
	public void setList2(List<L> list2) {
		this.list2 = list2;
	}
	
	public double maxSimilarityAverage(){
		if (this.list1.size()==0||this.list2.size()==0)
			return 0;
		
		List<Integer> iCheck = new ArrayList<Integer>();
		List<Integer> jCheck = new ArrayList<Integer>();
		List<Double> similist = new ArrayList<Double>();
		

		while (iCheck.size()<this.matrix.length && jCheck.size()<this.matrix[0].length){
			double max= this.matrix[0][0];
			int ix=0;
			int jx=0;
			double sim;
			for (int i=0; i<this.matrix.length;i++) 
				for (int j=0; j<this.matrix[0].length; j++) {
					if (!iCheck.contains(i)&&!jCheck.contains(j)){
						sim = this.matrix[i][j];
						if (Math.abs(sim)>Math.abs(max)) {
							max=sim;
							ix=i;
							jx=j;
						}
					}
					

				}
			iCheck.add(ix);
			jCheck.add(jx);
			similist.add(max);
		}
		
		
		double sumMax=0;
        for (double val : similist)
			sumMax+=val;


		return ((sumMax)*(this.list1.size()+this.list2.size()))/
				(2*this.list1.size()*this.list2.size());
		//return (((sumMax)/similist.size())/(Math.abs(this.list1.size()-this.list2.size())+1));
	}

	

}
